/**
 * Copyright &copy; 2020 <a href="https://github.com/somowhere/exam-system">exam-system</a> All rights reserved.
 */
package com.shanhesoft.java.modules.exam.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shanhesoft.java.common.core.util.BeanUtil;
import com.shanhesoft.java.common.core.util.ObjectUtil;
import com.shanhesoft.java.common.core.util.StringUtil;
import com.shanhesoft.java.common.core.vo.PageModel;
import com.shanhesoft.java.common.persistence.service.impl.DataServiceImpl;
import com.shanhesoft.java.modules.exam.domain.OjSubmissions;
import com.shanhesoft.java.modules.exam.domain.OjSubmissionsHistory;
import com.shanhesoft.java.modules.exam.domain.dto.OjSubmissionsDto;
import com.shanhesoft.java.modules.exam.domain.dto.OjSubmissionsQueryCriteria;
import com.shanhesoft.java.modules.exam.domain.vo.OjSubmissionsVo;
import com.shanhesoft.java.modules.exam.repository.OjSubmissionsRepository;
import com.shanhesoft.java.modules.exam.service.OjSubmissionsHistoryService;
import com.shanhesoft.java.modules.exam.service.OjSubmissionsService;

/**
 * 提交记录ServiceImpl 提交记录
 * @author admin
 * @version 2020-09-26 20:35:59
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OjSubmissionsServiceImpl extends DataServiceImpl<OjSubmissionsRepository, OjSubmissions, OjSubmissionsDto, String> implements OjSubmissionsService{

	@Autowired
	private JmsMessagingTemplate jmsMessagingTemplate;

	@Autowired
	private OjSubmissionsHistoryService ojSubmissionsHistoryService;

	@Value("${spring.activemq.send-destination-name}")
    private String sendDestinationName;

	@Override
	public void saveOrUpdate(OjSubmissionsDto ojSubmissionsDto) {
		QueryWrapper<OjSubmissions> queryWrapper = new QueryWrapper<>();
		String courseId = ojSubmissionsDto.getCourseId();
		if(StringUtil.isNotBlank(courseId)) {
			queryWrapper.eq("course_id", courseId);
		}
		String answerId = ojSubmissionsDto.getAnswerId();
		if(StringUtil.isNotBlank(answerId)) {
			queryWrapper.eq("answer_id", answerId);
		}
		String problemId = ojSubmissionsDto.getProblemId();
		if(StringUtil.isNotBlank(problemId)) {
			queryWrapper.eq("problem_id", problemId);
		}
		String questionBankId = ojSubmissionsDto.getQuestionBankId();
		if(StringUtil.isNotBlank(questionBankId)) {
			queryWrapper.eq("question_bank_id", questionBankId);
		}
		String uid = ojSubmissionsDto.getUid();
		if(StringUtil.isNotBlank(uid)) {
			queryWrapper.eq("uid", uid);
		}
		OjSubmissions ojSubmissions = getOne(queryWrapper);
		if(ObjectUtil.isNotNull(ojSubmissions)) {
			OjSubmissionsHistory ojSubmissionsHistory = new OjSubmissionsHistory();
			BeanUtil.copyProperties(ojSubmissions, ojSubmissionsHistory);
			ojSubmissionsHistoryService.saveOrUpdate(ojSubmissionsHistory);
			this.removeById(ojSubmissions.getId());
		}
		super.saveOrUpdate(ojSubmissionsDto);
		Map<String, Object> mapMessage = new HashMap<>();
		mapMessage.put("event", "SubmissionCreated");
		mapMessage.put("submissionId", ojSubmissionsDto.getId());
		jmsMessagingTemplate.convertAndSend(sendDestinationName, mapMessage);
	}

	@Override
	public OjSubmissionsDto saveOrUpdateReturn(OjSubmissionsDto ojSubmissionsDto) {
		this.saveOrUpdate(ojSubmissionsDto);
		return  ojSubmissionsDto;
	}

	@Override
	public IPage<OjSubmissionsVo> getListPage(PageModel pm, QueryWrapper wrapper) {
		return repository.findSubmissionVoPage(pm, wrapper);
	}

	@Override
	public OjSubmissionsVo findVoById(String id) {
		return repository.findSubmissionVoById(id);
	}

	@Override
	public IPage<Map<String, Object>> getUserList(PageModel pm, OjSubmissionsQueryCriteria ojSubmissionsQueryCriteria) {
		return repository.findSubmissionUserVoById(pm);
	}
}
